package com.my.system.dao.customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.my.system.dao.DataBaseConnection;
import com.my.system.object.client.Client;

public class ClientDao {
	private static Connection con = null;
	private static PreparedStatement psta = null;
	private static ResultSet rs = null;
	static {
		con = DataBaseConnection.getConnection();
	}

	public static boolean isClientExsit(String name) {
		String sql = "select * from client where name=?";
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, name);
			rs = psta.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}

	public static Client searchClient(String name) {
		String sql = "select * from client where name=?";
		Client client = null;
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, name);
			rs = psta.executeQuery();
			if (rs.next()) {
				int id = rs.getInt(1);
				String tname = rs.getString(2);
				String phone = rs.getString(3);
				int client_type = rs.getInt(4);
				int clent_status = rs.getInt(5);
				client = new Client(id, tname, phone, client_type, clent_status);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return client;
	}
	
	public static Client searchClientByID(int id1) {
		String sql = "select * from client where id=?";
		Client client = null;
		try {
			psta = con.prepareStatement(sql);
			psta.setInt(1, id1);
			rs = psta.executeQuery();
			if (rs.next()) {
				int id = rs.getInt(1);
				String tname = rs.getString(2);
				String phone = rs.getString(3);
				int client_type = rs.getInt(4);
				int clent_status = rs.getInt(5);
				client = new Client(id, tname, phone, client_type, clent_status);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return client;
	}

	public static boolean insertClient(Client client) {
		String sql = "insert into client(" + "name," + "phone,"
				+ "client_type_id," + "client_status_id) " + "values(?,?,?,?)";
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, client.getName());
			psta.setString(2, client.getPhone());
			psta.setInt(3, client.getClient_type());
			psta.setInt(4, client.getClent_status());
			con.setAutoCommit(false);
			int i = psta.executeUpdate();
			con.commit();
			con.setAutoCommit(true);
			if (i > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}

}
